<template>
  <ele-page
    flex-table
    :multi-card="false"
    hide-footer
    style="min-height: 420px"
  >
    <ele-card
      flex-table
    >
      <ele-split-panel
        ref="splitRef"
        flex-table
        size="256px"
        allow-collapse
        :custom-style="{ borderWidth: '0 1px 0 0', padding: '5px 0' }"
        :body-style="{ padding: 0, overflow: 'hidden' }"
        :style="{ height: '100%', overflow: 'visible' }"
      >
        <div style="padding: 0 16px 12px 0">
          <el-input
            v-model="keyword"
            clearable
            :maxlength="20"
            placeholder="输入关键字搜索"
            :prefix-icon="SearchOutlined"
          />
        </div>
        <ele-loading :loading="loading" class="left-tree">
          <el-tree
            ref="treeRef"
            :data="treeList"
            highlight-current
            node-key="id"
            :props="{ label: 'name' }"
            :expand-on-click-node="false"
            :default-expand-all="true"
            :filter-node-method="filterNode"
            @node-click="onNodeClick"
          >
            <template #default="{ data: d }">
              <div>{{ d.name }}</div>
            </template>
          </el-tree>
        </ele-loading>
        <template #body>
          <PageList v-if="current?.id" :objId="current.id" :supplierList="treeList" />
        </template>
      </ele-split-panel>
    </ele-card>
  </ele-page>
</template>

<script setup>
import { listSupplier } from '@/api/common'

import { SearchOutlined } from '@/components/icons'
import { useMobile } from '@/utils/use-mobile'

import { ref } from 'vue'

import PageList from './components/list.vue'

/** 是否是移动端 */
const { mobile } = useMobile()

const splitRef = useTemplateRef('splitRef')
const treeRef = useTemplateRef('treeRef')

/** 当前编辑数据 */
const current = ref({})

const keyword = ref('')

const loading = ref(false)

const treeList = ref([])

/** 树过滤方法 */
function filterNode(value, data) {
  if (value)
    return !!(data.name && data.name.includes(value))

  return true
}

watch(() => keyword.value, (value) => {
  treeRef.value?.filter?.(value)
})

/** 选择数据 */
function onNodeClick(row) {
  // 移动端自动收起左侧
  if (current.value != null && mobile.value)
    splitRef.value?.toggleCollapse?.(true)

  if (row && row.id) {
    current.value = row
    treeRef.value?.setCurrentKey?.(row.id)
  }
  else {
    current.value = null
  }
}

/** 查询 */
function query() {
  loading.value = true
  listSupplier()
    .then((res) => {
      treeList.value = res.data ?? []
      nextTick(() => {
        onNodeClick(treeList.value[0])
      })
    })
    .finally(() => {
      loading.value = false
    })
}

query()
</script>

<style lang="scss" scoped>
.left-tree {
    flex: 1;
    padding-right: 16px;
    overflow: auto;

    :deep(.el-tree-node__content) {
      height: 40px;
      line-height: 40px;
      border-radius: calc((var(--el-border-radius-small) - 2px) * 3);
      transition: all 0.2s;

      & > .el-tree-node__expand-icon {
        padding: 2px;
      }
    }

    :deep(.el-tree-node.is-current > .el-tree-node__content) {
      color: var(--el-color-primary);
      font-weight: bold;
    }
  }
</style>
